<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<jsp:directive.page import="java.util.Date"/>
<jsp:directive.page import="java.math.BigInteger"/>
<jsp:directive.page import="java.text.DecimalFormat"/>
<jsp:directive.page import="java.util.Calendar"/>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>Shri Ballaleshwar Enterprises --- Home Page </title>
    
	 
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="SHORTCUT ICON" href="Images/favicon.ico">
	<link rel="stylesheet" type="text/css" href="csschart.css">
	
  </head>
  
  <body >
    <table align="center">
	<tr >
		<td >
      <h1 id="test" >Company Wise Current Stock Graph</h1>
			<ul class="yAxis" >
			<%for(int i=250000;i>=25000;i=i-25000) 
			{%>
				<li><%=i %></li>

			<%} %>
			</ul>
	
		
     <dl id="csschart" >
      <%
    
    
   //******IMP****	as we insert daily stock in the database table(datewise_stock_report),in that we enter  stock 
   //  when first time any one log in ,means stock entered for today is actual stock of the last day(yesterday)  */
      			String companyid=""+application.getAttribute("company_id");
  			//and owner_company_code="+companyid
    String stock_frm_date_f="", stock_to_date_f="";
     String frm_date_f="", to_date_f="";
	float total_quantity_of_last_month=0,total_purchase_this_month=0,total_sale_this_month=0,total_current_stock=0;
	BigInteger total_pending_purchase_payment=new BigInteger("0");
	BigInteger total_pending_sale_payment=new BigInteger("0");
		DecimalFormat df=new DecimalFormat("#.##");

		int srcount=1;
    	Connection con=null;
    	ServletContext context=config.getServletContext();
		con=(Connection)context.getAttribute("dbcon");
		PreparedStatement prst1=null,prst2=null,prst3=null,prst4=null,prst5=null,prst6=null,prst7=null,prst8=null,prst9=null,prst10=null;
		ResultSet rs1=null,rs2=null,rs3=null,rs4=null,rs5=null,rs6=null,rs7=null,rs8=null,rs9=null,rs10=null;
		String arr_month[]={"JAN","FEB","MAR","APR","MAY","JUNE","JULY","AUG","SEPT","OCT","NOV","DEC"};
		int count_rs2=0;
		boolean date_flag=true;
		String sd_date="";
		float final_company_total=0;
		try{
		   Date today_date=new Date();
  		   prst9=con.prepareStatement("select CURRENT_DATE");    	 
		   rs9=prst9.executeQuery();
			while(rs9.next())
			{
		 		today_date=rs9.getDate(1);
			}
	   		String sample_date=""+today_date;
	   		
	   						String arr_sale_default_date[]=new String[4];
						arr_sale_default_date=today_date.toString().split("-");
						//out.println(""+arr_sale_default_date[2]+"-"+arr_sale_default_date[1]+"-"+arr_sale_default_date[0]);
			String str_sale_default_date=arr_sale_default_date[2]+"-"+arr_sale_default_date[1]+"-"+arr_sale_default_date[0];
	//	out.println("str_sale_default_date"+str_sale_default_date+"<br>");
		
		int month=Integer.parseInt(request.getParameter("month"));
    	int year=Integer.parseInt(request.getParameter("year"));  
    	
    	
    	  
   		Date d=new Date();
		Calendar cale=Calendar.getInstance();//get instance of calendar
		cale.set(Calendar.MONTH,month);//set current month as calendar month
		int lastdate=cale.getActualMaximum(Calendar.DATE);//set current date to calculate final date of the current month
    	int firstdate=cale.getActualMinimum(Calendar.DATE);//set current date to calculate first date of the current month
    	
    	
    	String month_in_words=arr_month[month];
    	sd_date=lastdate+"-"+(month+1)+"-"+year;
    	//out.println(lastdate+"-"+(month+1)+"-"+year+"Record for month 	"+month_in_words+"<br>");		
   		
// as  month array starts from 0 -jan 1- feb,to get correct month we add +1 (to get current month) add +2 (to get next month) Accordingly
		 stock_frm_date_f=year+"-"+(month+1)+"-"+(firstdate+1);//stock of first day of month is actual stock of last date of previous month so to get stock of current month date incremented
		 stock_to_date_f=year+"-"+(month+2)+"-"+firstdate;
		 
		 frm_date_f=year+"-"+(month+1)+"-"+firstdate;//for getting details of sale_part_payment and purchase_part_payment as these entries according to date
		 to_date_f=year+"-"+(month+1)+"-"+lastdate;//for getting details of sale_part_payment and purchase_part_payment
		 
		//out.println("from date "+stock_frm_date_f+"......to date "+stock_to_date_f+"<br>");
		//out.println("from date "+frm_date_f+"......to date "+to_date_f+"<br>");
		
		//by dheeraj and hemant on 4-2-2011 at 1pm
		//**************if last date of month entry not present in the datewise stock table ,then get next subsequent
		 //date record(means consider stock not entered for some days,then stock entered after gap is equal to stock
		 // for the next date) is stock of the last date of the month********************************************/
		 //and as we mentioning again Stock present in the "datewise_stock_report" table  where each entry for date [i] represents
		 // stock of date[i-1],stock entered today is actually stock of yesterday
		try
		{
		boolean check_date_present_flag=true;
		PreparedStatement we=con.prepareStatement("SELECT stock_date FROM datewise_stock_report WHERE STATUS='V' AND stock_date BETWEEN ? AND (SELECT CURRENT_DATE) and owner_company_code=? GROUP BY(stock_date) ");
		we.setString(1,stock_to_date_f);
		we.setString(2,companyid);
		ResultSet wers=we.executeQuery();
		while(wers.next())
		{
		if(check_date_present_flag)
			{
				stock_to_date_f=wers.getString("stock_date");
				check_date_present_flag=false;
			}
		}
		}
		catch(NullPointerException e)
		{
		out.println("in getting next date<br>"+e);
		}
		catch(Exception ee)
		{
		out.println("Exception <br>"+ee);
		}		
/************Finish getting next date when last date record is not present for particular month***********************/
			
		prst1=con.prepareStatement("select company_code,company_name from purchasecompany  where status='V' and owner_company_code="+companyid);
		rs1=prst1.executeQuery();
		while(rs1.next())
		{
		int date_diff=0;
		final_company_total=0;
			PreparedStatement  prsew2=con.prepareStatement("SELECT DATEDIFF(?,?)");
			prsew2.setString(1,stock_to_date_f);
			prsew2.setString(2,sample_date);
		  	ResultSet rstew2=prsew2.executeQuery();
			while(rstew2.next())
		{
		
		date_diff=rstew2.getInt(1);
		}
		
		if(date_diff>0)
		{
			if(date_flag)
			{
			out.println("Opening stock -----  No record Available ");
			date_flag=false;
			}
		}
		else
		{		
		
			prst2=con.prepareStatement("SELECT stock_date,datewise_stock_report.mainproduct_code,subproduct_code,stock_on_stock_date FROM  mainproduct,datewise_stock_report WHERE stock_date=? AND mainproduct.mainproduct_code=datewise_stock_report.mainproduct_code and  mainproduct.owner_company_code=datewise_stock_report.owner_company_code AND datewise_stock_report.STATUS='V' AND purchasecompany_code=? and owner_company_code=?");
			prst2.setString(1,stock_to_date_f);
			prst2.setString(2,rs1.getString("company_code"));		
			prst2.setString(3,companyid);	
			rs2=prst2.executeQuery();
				while(rs2.next())
				{
		/**************************display stock report of current date****1*2*2011*by dheeraj**********************************/
					prst3=con.prepareStatement("SELECT purchase_price,sub_pro_name,vat FROM subproduct where mainproduct_code=? and subproduct_code=? and  status='V' and owner_company_code=?");//this query is for getting products & stock according to company
					prst3.setInt(1,rs2.getInt("mainproduct_code"));
					prst3.setInt(2,rs2.getInt("subproduct_code"));
					prst3.setString(3,companyid);
					rs3=prst3.executeQuery();
					prst4=con.prepareStatement("SELECT purchasecompany_code FROM subproduct,mainproduct  WHERE  subproduct.mainproduct_code=mainproduct.mainproduct_code and subproduct.owner_company_code=mainproduct.owner_company_code and owner_company_code="+companyid+"  GROUP BY(purchasecompany_code)");//this query is for getting total amount of stock per company
					rs4=prst4.executeQuery();

					float net_amount=0;
					float vat_amount=0;
					float amount=0;
					while(rs3.next())
					{
					String sub_pro_name=rs3.getString("sub_pro_name");
					String stock_quantity=rs2.getString("stock_on_stock_date");
					String purchase_price=rs3.getString("purchase_price") ;
					amount=Float.parseFloat(stock_quantity)*Float.parseFloat(""+purchase_price);
					String vat=rs3.getString("vat");
					vat_amount=(Float.parseFloat(""+amount)*Float.parseFloat(""+vat))/100;
					net_amount=Float.parseFloat(""+amount)+Float.parseFloat(""+vat_amount);
					 %>

					 <%	
					 
					final_company_total=final_company_total+net_amount;
					}
		/**************finish************display stock report of current date*****above****************************/

				}
					srcount=1;			
				
					while(rs4.next())
					{
					if(rs4.getInt("purchasecompany_code")==rs1.getInt(1))//to get total of particular company
					{
					//out.println(final_company_total+"-----");	
					float cal=(final_company_total*100)/250000;
					//Math.round(cal);
					
				String graph_percentage="0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100";
				//out.println(final_company_total+"1-----");	
				
				String match="6.8";
				//out.println(Integer.parseInt(match)+"-----"+cal);
				String graph_array[]=new String[100];
				//String frm_date_f="";
				for(int i=0;i<graph_array.length;i++)
				{
					graph_array=graph_percentage.split(",");
					//out.println(graph_array[i]+"<br>");
					if(graph_array[i].equals(""+Math.round(cal)))
					{
						%>
						<dd class="p100"><span></span></dd>
						<dd class="sub p<%= graph_array[i]%>"><span></span></dd>
						<dd class="p0"><span></span></dd>
							
						<%//out.println(graph_array[i]+"<br>");
					}
						%>
						
						<%
				}
			
					}
				}		%>
				
				
					<%
					}
		}
		%> 
		</dl>
		<ul class="xAxis">
		<li></li>
		<%
		prst5=con.prepareStatement("select company_code,company_name from purchasecompany  where status='V' and owner_company_code="+companyid);
		rs5=prst5.executeQuery();
		while(rs5.next())
		{
			%>
				
				<li><%= rs5.getInt("company_code") %> </li>
				<li></li>
			<%
		}
		%></ul><%
			}
     	catch(NullPointerException e){out.println("<br>No details available stock<br>");}	
     	catch(Exception e){out.println("<br>Exception<br>"+e);}	
     	%>
     	</td>
	</tr>
	</table>
<br><br>

		</body>
		</html>